//
//  MeViewController.m
//  滴滴快拍
//
//  Created by dllo on 16/5/18.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "MeViewController.h"

#import "NoLoginTableCell.h"
#import "NoLoginContentTableCell.h"

#import "AttentionViewController.h"
#import "FavoriteViewController.h"
#import "DownloadViewController.h"
#import "VideoViewController.h"
#import "AGLStoreViewController.h"

#import "YYShareView.h"
#import "LookViewController.h"

#import "AppDelegate.h"
#import "WeiboSDK+Statistics.h"
#import "StatisticsDemoRootViewController.h"
#import "UIImageView+WebCache.h"
@interface MeViewController ()
<
UITableViewDelegate,
UITableViewDataSource,
SendUserDelegate

>

{
    WBSDKRelationshipButton *relationshipButton;
    WBSDKCommentButton *commentButton;
}



@property (nonatomic, strong)NSString *screenname;
@property (nonatomic,strong)NSString *picture;
@end

@implementation MeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"我";
    self.view.backgroundColor = [UIColor colorWithRed:232 / 255.0 green:232 / 255.0 blue:232 / 255.0 alpha:1];
    
    [self createView];
    
    AppDelegate *myDelegate =(AppDelegate*)[[UIApplication sharedApplication] delegate];
    myDelegate.delegate = self;
    
    
    
}

- (void)sendValue:(WeiboUser *)user {
    self.userDic = user;
    [self.tableView reloadData];
}


- (void)createView {
    self.tableView = [[BaseTableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 64 - 49) style:UITableViewStylePlain];
    self.tableView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:self.tableView];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    //    self.tableView.scrollEnabled = NO;
    [self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    [self.tableView registerClass:[NoLoginTableCell class] forCellReuseIdentifier:NSStringFromClass([NoLoginTableCell class])];
    [self.tableView registerClass:[NoLoginContentTableCell class] forCellReuseIdentifier:NSStringFromClass([NoLoginContentTableCell class])];
    self.tableView.bounces = NO;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    BaseView *view = [[BaseView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 20)];
    view.backgroundColor = [UIColor colorWithRed:232 / 255.0 green:232 / 255.0 blue:232 / 255.0 alpha:1];
    return view;
}

- (nullable NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    return @" ";
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 7;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)   indexPath {
    NSArray *arr = @[@"关注",@"赞过的视频",@"下载的视频",@"录制的视频", @"收藏的视频",@"清除缓存"];
    NSArray *picArr = @[@"guanzhu.png",@"zan.png",@"download.png",@"shangchuan.png",@"zan.png",@"clear.png"];
    
    if (indexPath.section == 0) {
        NoLoginTableCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([NoLoginTableCell class]) forIndexPath:indexPath];
        if (self.userDic == nil) {
            cell.picImageView.image = [UIImage imageNamed:@"NoLogin.png"];
            cell.titleLabel.text = @"未登录";
            cell.contentLabel.text = @"登录查看你的精彩空间";
            cell.goImageView.image = [UIImage imageNamed:@"Go.png"];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            
        }else {
            NSString *str = self.userDic.profileImageUrl;
            cell.titleLabel.text = self.userDic.screenName;
            cell.contentLabel.text = self.userDic.userDescription;
            [cell.picImageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@", str]]];
        }
        
        
        
        return cell;
        
        
    }else {
        NoLoginContentTableCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([NoLoginContentTableCell class]) forIndexPath:indexPath];
        cell.titleLabel.text = arr[indexPath.section - 1];
        cell.picImageView.image = [UIImage imageNamed:picArr[indexPath.section - 1]];
        cell.goImageView.image = [UIImage imageNamed:@"Go.png"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        return cell;
        
    }
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        return 80;
    }else {
        return 40;
    }
    
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        NSLog(@"登录");
        YYShareView * share = [YYShareView creatXib];
        [share setGetTouch:^(int tag)
         {
             [self getTag:tag];
         }];
        [share show];
        
        
    }else if (indexPath.section == 1) {
        NSLog(@"关注");
        AttentionViewController *attentionVC = [[AttentionViewController alloc] init];
        [self.navigationController pushViewController:attentionVC animated:YES];
    }else if (indexPath.section == 2) {
        NSLog(@"赞");
        FavoriteViewController *favoriteVC = [[FavoriteViewController alloc] init];
        [self.navigationController pushViewController:favoriteVC animated:YES];
    }
        else if (indexPath.section == 3) {
        DownloadViewController *downVC = [[DownloadViewController alloc] init];
        [self.navigationController pushViewController:downVC animated:YES];
    }else if (indexPath.section == 4) {
        LookViewController *recordVC = [[LookViewController alloc] init];
        [self.navigationController pushViewController:recordVC animated:YES];
    } else if (indexPath.section == 5) {
        AGLStoreViewController *storeVC = [[AGLStoreViewController alloc] init];
        [self.navigationController pushViewController:storeVC animated:YES];
    }else {
         [self clearFile];
    }
    
}


- (void)getTag:(int)tag {
    if (tag == 6) {
        WBAuthorizeRequest *request = [WBAuthorizeRequest request];
        request.redirectURI = @"https://api.weibo.com/oauth2/default.html";
        request.scope = @"all";
        request.userInfo = @{@"SSO_From": @"SendMessageToWeiboViewController",
                             @"Other_Info_1": [NSNumber numberWithInt:123],
                             @"Other_Info_2": @[@"obj1", @"obj2"],
                             @"Other_Info_3": @{@"key1": @"obj1", @"key2": @"obj2"}};
        [WeiboSDK sendRequest:request];
        
    }else {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"等等~" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
        [alert show];
    }
    
    
    AppDelegate *myDelegate =(AppDelegate*)[[UIApplication sharedApplication] delegate];
    
    
    
}


// 清除缓存
- (long)fileSizeAtPath:(NSString *)filePath {
    
    NSFileManager *manager = [NSFileManager defaultManager];
    if ([manager fileExistsAtPath:filePath]) {
        return [[manager attributesOfItemAtPath:filePath error:nil] fileSize];
    }
    return 0;
}

- ( float ) folderSizeAtPath:( NSString *) folderPath{
    
    NSFileManager * manager = [ NSFileManager defaultManager ];
    
    if (![manager fileExistsAtPath :folderPath]) return 0 ;
    
    NSEnumerator *childFilesEnumerator = [[manager subpathsAtPath :folderPath] objectEnumerator ];
    
    NSString * fileName;
    
    long long folderSize = 0 ;
    
    while ((fileName = [childFilesEnumerator nextObject ]) != nil ){
        
        NSString * fileAbsolutePath = [folderPath stringByAppendingPathComponent :fileName];
        
        folderSize += [ self fileSizeAtPath :fileAbsolutePath];
        
    }
    
    return folderSize/( 1024.0 * 1024.0 );
    
}

- ( float )filePath

{
    
    NSString * cachPath = [ NSSearchPathForDirectoriesInDomains ( NSCachesDirectory , NSUserDomainMask , YES ) firstObject ];
    
    return [ self folderSizeAtPath :cachPath];
    
}

// 清理缓存

- ( void )clearFile

{
    
    NSString * cachPath = [ NSSearchPathForDirectoriesInDomains ( NSCachesDirectory , NSUserDomainMask , YES ) firstObject ];
    
    NSArray * files = [[ NSFileManager defaultManager ] subpathsAtPath :cachPath];
    
    NSLog ( @"cachpath = %@" , cachPath);
    
    for ( NSString * p in files) {
        
        NSError * error = nil ;
        
        NSString * path = [cachPath stringByAppendingPathComponent :p];
        
        if ([[ NSFileManager defaultManager ] fileExistsAtPath :path]) {
            
            [[ NSFileManager defaultManager ] removeItemAtPath :path error :&error];
            
        }
        
    }
    
    [ self performSelectorOnMainThread : @selector (clearCachSuccess) withObject :nil waitUntilDone : YES ];
    
}

- ( void )clearCachSuccess

{
    
    NSLog ( @" 清理成功 " );
    
    UIAlertView * alertView = [[ UIAlertView alloc ] initWithTitle : @" 提示 " message : [NSString stringWithFormat:@"清理缓存%.2fM",[self filePath]] delegate : nil cancelButtonTitle : @" 确定 " otherButtonTitles : nil ];
    
    [alertView show ];
    
    [ _tableView reloadData ];//清理完之后重新导入数据
    
}











- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
